package com.mayisoftware.mayioa.business.personal.impl.controller;


import com.github.pagehelper.PageInfo;
import com.mayisoftware.mayioa.business.flow.api.service.IFlowBaseService;
import com.mayisoftware.mayioa.business.personal.api.domain.PersonalSign;
import com.mayisoftware.mayioa.business.personal.api.service.IPersonalSignService;
import com.mayisoftware.mayioa.common.api.constant.Constants;
import com.mayisoftware.mayioa.platform.api.domain.SysUser;
import com.mayisoftware.mayioa.platform.impl.util.ShiroUtils;
import com.mayisoftware.mayioa.platform.impl.web.base.BaseController;
import com.mayisoftware.mayioa.platform.impl.web.page.TableDataInfo;
import liquibase.exception.RollbackFailedException;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.flowable.variable.api.history.HistoricVariableInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 用户报名控制器
 */
@Controller
@RequestMapping("/personal/apply")
public class PersonalApplyController extends BaseController{
    /**
     * URL前缀
     */
    private String prefix = "personal/apply";

    /**
     * 用户报名相关服务
     */
    @Autowired
    private IPersonalSignService personalSignService;

    @Autowired
    private IFlowBaseService flowBaseService;

    @RequiresPermissions("personal:apply:view")
    @GetMapping()
    public String view()
    {
        return prefix + "/apply";
    }

    @RequiresPermissions("personal:apply:list")
    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(PersonalSign personApply)
    {
//        startPage();

        SysUser user = ShiroUtils.getSysUser();
        personApply.setPerSignUserId(user.getUserId().intValue());
        List<PersonalSign> result = new ArrayList<>();
        List<PersonalSign> personApplies = personalSignService.queryApplyInfo(personApply);

        List<PersonalSign> personAppliAll = personalSignService.queryApplyInfo(personApply);
        System.out.println(personAppliAll.size());
        int num = 0;
        for (PersonalSign apply:personApplies){
            //没有流程ID信息
            if (StringUtils.isEmpty(apply.getProcessId())){
                continue;
            }


            apply.setCurrentStatus("进行中");

            HistoricProcessInstance pi = flowBaseService.queryProcessInfo(apply.getProcessId());

            if(flowBaseService.isFinished(apply.getProcessId())){
                apply.setCurrentStatus("已结束");


//                List<HistoricVariableInstance> variableInstances =flowBaseService.queryProcessVariableInfos(apply.getProcessId());
//
//                if (!CollectionUtils.isEmpty(variableInstances )){
//                    for (HistoricVariableInstance historicVariableInstance:variableInstances){
//                        if (historicVariableInstance.getVariableName().equalsIgnoreCase("outcome")){
//                            String resInfo = (String)historicVariableInstance.getValue();
//                            if (resInfo.equalsIgnoreCase("通过")){
//                                apply.setResult("1");
//                            }
//                            else if(resInfo.equalsIgnoreCase("驳回") ){
//                                apply.setResult("0");
//                            }
//                            break;
//                        }
//                    }
//
//                }
            }

            apply.setProcessName(pi.getProcessDefinitionName());
            if (null != pi.getStartTime()){
                apply.setFlowStartTime(Constants.YMD_HMS_FMT.format(pi.getStartTime()));
            }

            if (null != pi.getEndTime()){
                apply.setFlowEndTime(Constants.YMD_HMS_FMT.format(pi.getEndTime()));
            }


            if (!StringUtils.isEmpty(personApply.getProcessName()) && !pi.getProcessDefinitionName().contains(personApply.getProcessName())){
                continue;
            }
            else {
                result.add(apply);
                ++num;
            }
        }

        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(result);
        rspData.setTotal(num);
        return rspData;
    }

    /**
     * 审批历史
     */
    @GetMapping("/check")
    public String check(ModelMap mmap)
    {
        return prefix + "/check";
    }

}
